<template>
  <div :style="commentAreaStyle">
    <div class="comment-area-title">
      评论区
    </div>
    <div class="comment-area-divider"></div>
    <div class="comment-area-body">
      <div v-for="(item, index) in commentList" class="comment-area-main-item">
        <img class="comment-area-main-item-avatar" :src="avatarMap[item.userRole.toString()]"/>
        <div class="comment-area-main-item-comment-container">
          <div class="comment-area-main-item-comment-title">
            {{item.userId}}
          </div>
          <div class="comment-area-main-item-comment-content">
            {{item.content}}
          </div>
          <div class="comment-area-main-item-comment-attach">
            <div class="comment-area-main-item-comment-attach-time">
              {{item.createTime}}
            </div>
            <div @click="tryReply(index, item)" class="comment-area-main-item-comment-attach-reply">
              回复
            </div>
          </div>
          <div class="comment-area-main-item-comment-subreply">
            <div v-show="item.replyCount>0&&!replyCollection[index].showReply" class="comment-area-main-item-comment-subreply-count">
              共{{item.replyCount}}条回复, <span @click="showReply(index, item, 1)" :style="{color: '#00a1d6', cursor: 'pointer'}">点击查看</span>
            </div>
            <div v-show="replyCollection[index].showReply" class="comment-area-main-item-comment-reply-list">
              <div v-for="(subitem, subindex) in replyCollection[index].replyList" class="comment-area-main-item-comment-reply-item">
                <img class="comment-area-main-item-comment-reply-item-avatar" :src="avatarMap[subitem.userRole.toString()]"/>
                <div class="comment-area-main-item-comment-reply-item-content">
                  <span :style="{fontSize: '10px',fontWeight: 'bold',marginRight: '3px' }">
                    {{subitem.replyUserId ? subitem.userId+' 回复 '+subitem.replyUserId : subitem.userId}}
                  </span>
                  {{subitem.content}}
                  <div class="comment-area-main-item-comment-reply-item-attach">
                    <span :style="{marginRight: '8px'}">{{dateFormat("YYYY-mm-dd HH:MM", subitem.createTime)}}</span>
                    <span @click="tryReply(index, subitem)" :style="{cursor: 'pointer'}">回复</span>
                  </div>
                </div>
              </div>
              <el-pagination
                :style="{padding: '0px', position: 'relative', left: '-5px'}"
                small
                @current-change="(page)=>{subPageChange(page,index,item)}"
                layout="prev, pager, next"
                :page-size="5"
                :total="item.replyCount">
              </el-pagination>
            </div>
          </div>
          <div v-show="replyTargetPos==index" class="comment-area-main-item-comment-replyarea">
            <el-input show-word-limit :maxlength="128" class="comment-area-main-item-comment-replyarea-input" type="textarea" v-model="replyInput"></el-input>
            <div @click="pushReply" class="comment-area-main-item-comment-replyarea-btn">
              发表评论
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="comment-area-pagination-container">
      <el-pagination
        background
        @current-change="pageChange"
        layout="prev, pager, next"
        :total="commentCount">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import {avatarMap, ROLE_KEY, ROLE_MAP, UID_KEY} from "../../constants";
  import {getMainReplyList, getSubReplyList, pushSubComment, replySubComment} from "../../api/reply";
  import {dateFormat} from "../../utils";

  export default {
    name: "comment-area",
    data() {
      return {
        avatarMap,
        avatar: avatarMap[localStorage.getItem(ROLE_KEY)],
        commentList: [],
        replyCollection: [],
        dateFormat,
        replyTargetPos: -1,
        replyInput: '',
        replyTargetItem: null,
        commentCount: 0
      }
    },
    props: {
      commentAreaStyle: Object
    },
    methods :{
      tryReply(index, item) {
        this.replyTargetPos = index;
        this.replyTargetItem = item;
      },
      showReply(index, item, page) {
        const data = {
          id: item.id,
          page
        }
        getSubReplyList(data).then(res => {
          this.replyCollection[index].replyList = res.data
          this.replyCollection[index].showReply = true
        })
      },
      pushReply() {
        let item = this.replyTargetItem;
        if(item == null)
          return;
        if(item.paperId) {
          //回复mainComment
          const data = {
            parentId: item.id,
            content: this.replyInput,
            uid: localStorage.getItem(UID_KEY),
            role: ROLE_MAP[localStorage.getItem(ROLE_KEY)]
          }
          pushSubComment(data).then(res => {
            location.reload()
          })
        }
        else {
          let parentItem = this.commentList[this.replyTargetPos]
          let item = this.replyTargetItem;
          const data = {
            replyId: item.id,
            replyUserId: item.userId,
            parentId: parentItem.id,
            content: this.replyInput,
            uid: localStorage.getItem(UID_KEY),
            role: ROLE_MAP[localStorage.getItem(ROLE_KEY)]
          };
          replySubComment(data).then(res => {
            location.reload()
          })
        }
      },
      pageChange(page) {
        console.log(page)
        this.setCommentList(page)
      },
      subPageChange(page, index, item) {
        console.log(page, index, item)
        this.showReply(index, item, page)
        // this.replyCollection[index].replyList =
      },
      setCommentList(page) {
        const data = {
          pid: this.$route.params.id,
          page: page
        };
        getMainReplyList(data).then(res => {
          this.commentList = res.data.commentList;
          this.commentCount = parseInt(res.data.count)
          for(let i=0; i<this.commentList.length; ++i) {
            this.commentList[i].createTime = dateFormat("YYYY-mm-dd HH:MM", this.commentList[i].createTime)
            let obj = {
              showReplyList: false,
              replyList: []
            };
            this.replyCollection.push(obj)
          }
        })
      }
    },
    mounted() {
      this.setCommentList(1)
    }
  }
</script>

<style lang="scss" scoped>
  .comment-area {
    &-title {
      font-weight: 600;
      text-align: center;
    }
    &-divider {
      background-color: gainsboro;
      height: 1px;
      margin-top: 8px;
      margin-bottom: 5px;
    }
    &-body {
      padding: 10px 0 10px 0;
      /*width: 90%;*/
      margin: auto;
      box-sizing: border-box;
    }
    &-pagination-container {
      text-align: center;
    }
  }
  .comment-area-main-item {
    display: flex;
    margin-bottom: 10px;
    &-avatar {
      width: 40px;
      height: 40px;
      border-radius: 20px;
      margin-right: 10px;
    }
    &-comment {
      &-container {
        /*width: 100%;*/
        /*flex: 1;*/
        /*width: 95%;*/
        border-bottom: 0.5px solid gainsboro;
        width: 0;
        flex: 1;
        padding: 5px 5px 10px 0px;
      }
      &-title {
        font-size: 10px;
      }
      &-content {
        font-size: 13px;
        padding-top: 5px;
        max-width: 100%;
        /*max-width: 98%;*/
        word-wrap: break-word;
        word-break:break-all
      }
      &-attach {
        font-size: 10px;
        margin-top: 5px;
        display: flex;
        &-time {
          color: #99a2aa;
          margin-right: 10px;
        }
        &-reply {
          color: #99a2aa;
          cursor: pointer;
        }
      }
      &-subreply {
        &-count {
          margin-top: 5px;
          font-size: 10px;
        }
      }
      &-reply-list {
        /*width: 90%;*/
        padding: 8px 0 0px 0;
      }
      &-reply-item {
        display: flex;
        margin-bottom: 10px;
        &-avatar {
          width: 20px;
          height: 20px;
          border-radius: 10px;
          margin-right: 5px;
        }
        &-content {
          width: 0;
          flex: 1;
          font-size: 10px;
          padding-top: 4px;
          word-wrap: break-word;
          word-break:break-all
        }
        &-attach {
          color: #99a2aa;
          margin-top: 5px;
        }
      }
      &-replyarea {
        width: 60%;
        display: flex;
        margin-top: 5px;
        align-items: center;
        &-input {
          margin-right: 5px;
        }
        &-btn {
          display: flex;
          align-items: center;
          min-width: 50px;
          max-width: 50px;
          height: 50px;
          text-align: center;
          background-color: #409EFF;
          box-sizing: border-box;
          padding: 5px;
          font-size: 14px;
          color: white;
          border-radius: 4px;
          cursor: pointer;
        }
      }
    }
  }
</style>
